﻿package mortal.game.scene3D.ai.ais
{
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.event.*;
    import mortal.game.control.subControl.*;
    import mortal.game.scene3D.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.base.*;
    import mortal.game.scene3D.ai.data.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.copy.petBreak.*;

    public class MoveAI extends AICommand
    {
        protected var _scene:GameScene3D;
        protected var _target:Object;
        protected var _targetPixPoint:Point;
        protected var _meRole:MovePlayer;
        protected var _range:int = 0;
        protected var _isShowGuidePath:Boolean;
        protected var _nextMoveTime:Number = 0;
        protected var _moveSplitTime:int = 400;
        protected var _isStoped:Boolean = true;

        public function MoveAI()
        {
            return;
        }// end function

        override public function start(param1:Function = null) : void
        {
            this._scene = _data.scene;
            this._meRole = _data.meRole;
            this._target = _data.target;
            _callback = param1;
            this._isStoped = false;
            this.updateTilePoint();
            if (this.isInRange())
            {
                this.stop();
                return;
            }
            FrEventDispatcher.instance.proxyAddEventListener(this._meRole, PlayerEvent.ENTITY_DEAD, this.onEntityDead, false, 0, true);
            FrEventDispatcher.instance.proxyAddEventListener(this._meRole, PlayerEvent.GIRD_WALK_END, this.onGirdWalkEnd, false, 0, true);
            FrEventDispatcher.instance.proxyAddEventListener(this._meRole, PlayerEvent.WALK_END, this.onWalkEnd, false, 0, true);
            this.sceneMove(this._targetPixPoint);
            return;
        }// end function

        override public function set data(param1:AIData) : void
        {
            super.data = param1;
            this._range = param1.range;
            return;
        }// end function

        override public function stop(param1:Boolean = true) : void
        {
            if (this._meRole)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._meRole, PlayerEvent.ENTITY_DEAD, this.onEntityDead);
                FrEventDispatcher.instance.proxyRemoveEventListener(this._meRole, PlayerEvent.GIRD_WALK_END, this.onGirdWalkEnd);
                FrEventDispatcher.instance.proxyRemoveEventListener(this._meRole, PlayerEvent.WALK_END, this.onWalkEnd);
                if (this._meRole.isMove)
                {
                    this._meRole.stopWalking(param1);
                }
            }
            this._isStoped = true;
            this._target = null;
            this._isShowGuidePath = false;
            this._range = 0;
            super.endAI();
            return;
        }// end function

        public function get target() : Object
        {
            return this._target;
        }// end function

        protected function sceneMove(param1:Point) : void
        {
            var _loc_3:MovePlayer = null;
            var _loc_4:Point = null;
            var _loc_5:Boolean = false;
            var _loc_6:Array = null;
            var _loc_7:AstarTurnPoint = null;
            var _loc_8:Array = null;
            var _loc_9:int = 0;
            var _loc_10:Object = null;
            var _loc_11:int = 0;
            var _loc_2:* = getTimer();
            if (_loc_2 >= this._nextMoveTime)
            {
                _loc_3 = PetBreakCopyUtil.player;
                _loc_4 = new Point(_loc_3.x2d, _loc_3.y2d);
                _loc_5 = true;
                _loc_6 = Scene3DClickProcessor.findPath(_loc_4.x, _loc_4.y, param1.x, param1.y, _loc_5);
                if (_loc_6.length > 0)
                {
                    _loc_9 = _loc_6.length - 1;
                    while (_loc_9 >= 0)
                    {
                        
                        _loc_10 = _loc_6[_loc_9];
                        if (_loc_10 is AstarTurnPoint)
                        {
                            _loc_7 = _loc_10 as AstarTurnPoint;
                            AIManager.addJump(new Point(_loc_7._x + 0.5, _loc_7._y + 0.5), true);
                        }
                        else
                        {
                            _loc_11 = 0;
                            if (_loc_9 == (_loc_6.length - 1))
                            {
                                _loc_11 = this._range;
                            }
                            AIManager.addMoveByPath(_loc_10 as Array, _loc_11, true);
                            if (_loc_9 == (_loc_6.length - 1))
                            {
                            }
                        }
                        _loc_9 = _loc_9 - 1;
                    }
                }
                this.stop();
            }
            return;
        }// end function

        protected function onEntityDead(event:Event) : void
        {
            this.stop();
            return;
        }// end function

        protected function onWalkEnd(event:Event) : void
        {
            if (this.isInRange())
            {
                this.stop();
            }
            return;
        }// end function

        protected function onGirdWalkEnd(event:Event) : void
        {
            if (this.isInRange())
            {
                this.stop();
            }
            return;
        }// end function

        protected function isInRange() : Boolean
        {
            var _loc_2:Number = NaN;
            if (this._target == null)
            {
                return true;
            }
            this.updateTilePoint();
            try
            {
                _loc_2 = GameMapUtil.getDistance(this._meRole.x2d, this._meRole.y2d, this._targetPixPoint.x, this._targetPixPoint.y);
            }
            catch (e)
            {
            }
            var _loc_1:* = _loc_2 <= this.range;
            return _loc_1;
        }// end function

        public function get range() : Number
        {
            if (this._range >= 0)
            {
                return this._range;
            }
            return this._meRole.speed.speed;
        }// end function

        public function set range(param1:Number) : void
        {
            this._range = param1;
            return;
        }// end function

        protected function updateTilePoint() : void
        {
            var _loc_1:* = PetBreakCopyUtil.player;
            if (this._target is Point)
            {
                this._targetPixPoint = Point(this._target);
                _loc_1.currentFollowTarget = null;
            }
            else
            {
                if (!this._targetPixPoint)
                {
                    this._targetPixPoint = new Point();
                }
                if (this._target != null)
                {
                    this._targetPixPoint.x = (this._target as IGame2D).x2d;
                    this._targetPixPoint.y = (this._target as IGame2D).y2d;
                }
                else
                {
                    this._targetPixPoint.x = _loc_1.x2d;
                    this._targetPixPoint.y = _loc_1.y2d;
                }
            }
            return;
        }// end function

    }
}
